#include "ipc.h"
#include <unistd.h>  // 确保包含unistd.h

void smoker(material_t my_material) {
    while (1) {
        sem_down(sem_id);
        //printf("Smoker %d: Got semaphore\n", my_material);
        if (shm_data->ready) {
            if (shm_data->materials[0] != my_material && shm_data->materials[1] != my_material) {
                printf("Smoker with %d is making a cigarette\n", my_material);
                shm_data->ready = 0;
                sem_up(sem_id);
               // printf("Smoker %d: Released semaphore\n", my_material);
                sleep(1);
                continue;
            }
        }
        sem_up(sem_id);
        //printf("Smoker %d: Released semaphore\n", my_material);
        sleep(1);
    }
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <material>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    material_t my_material = atoi(argv[1]);
    init_ipc();
    smoker(my_material);
    cleanup_ipc();
    return 0;
}
